package com.leetcode.algorithm.y22.m09.w4;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 1700. 无法吃午餐的学生数量
 * 
 * https://leetcode.cn/problems/number-of-students-unable-to-eat-lunch/
 * 
 * @author jie.deng
 *
 */
class Question1700Solution01 {
	
	public int countStudents(int[] students, int[] sandwiches) {
		int len = students.length;
		Deque<Integer> deque = new ArrayDeque<>(len);
		for (int i = 0; i < len; i++) {
			deque.offerLast(students[i]);
		}
		int idx = 0;
		while (idx < len) {
			boolean flag = false;
			int size = deque.size();
			for (int i = 0; i < size; i++) {
				Integer expected = deque.pollFirst();
				if (sandwiches[idx] == expected.intValue()) {
					idx++;
					flag = true;
				} else {
					deque.offerLast(expected);
				}
			}
			if (!flag) {
				break;
			}
		}
		return deque.size();
	}
	
}